package com.newshub.ai.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 问题分析服务
 * 基于规则分析问题类型，决定是否需要图表展示
 */
@Slf4j
@Service
public class QuestionAnalyzer {
    
    /**
     * 分析问题类型
     */
    public QuestionType analyzeQuestionType(String query) {
        if (query == null || query.trim().isEmpty()) {
            return QuestionType.UNKNOWN;
        }
        
        String lowerQuery = query.toLowerCase().trim();
        
        // 对比类问题
        if (containsAny(lowerQuery, "对比", "比较", "哪个多", "哪个少", "排名", "排序", "高低", "大小")) {
            log.debug("问题类型分析: {} -> 对比类", query);
            return QuestionType.COMPARISON;
        }
        
        // 分布类问题
        if (containsAny(lowerQuery, "分布", "占比", "比例", "构成", "组成", "份额", "百分比")) {
            log.debug("问题类型分析: {} -> 分布类", query);
            return QuestionType.DISTRIBUTION;
        }
        
        // 趋势类问题
        if (containsAny(lowerQuery, "趋势", "变化", "增长", "下降", "上升", "波动", "走势", "发展")) {
            log.debug("问题类型分析: {} -> 趋势类", query);
            return QuestionType.TREND;
        }
        
        // 详情类问题
        if (containsAny(lowerQuery, "内容", "详情", "具体", "是什么", "怎么样", "如何", "什么")) {
            log.debug("问题类型分析: {} -> 详情类", query);
            return QuestionType.DETAIL;
        }
        
        // 分析类问题
        if (containsAny(lowerQuery, "分析", "原因", "为什么", "怎么", "如何", "建议", "对策")) {
            log.debug("问题类型分析: {} -> 分析类", query);
            return QuestionType.ANALYSIS;
        }
        
        // 数量统计类问题（归类为对比类，适合图表）
        if (containsAny(lowerQuery, "数量", "统计", "多少", "几个", "几条", "总数")) {
            log.debug("问题类型分析: {} -> 数量统计类（归类为对比类）", query);
            return QuestionType.COMPARISON;
        }
        
        // 时间相关类问题（归类为趋势类，适合图表）
        if (containsAny(lowerQuery, "最近", "今天", "昨天", "本周", "本月", "今年", "每天", "每月")) {
            log.debug("问题类型分析: {} -> 时间相关类（归类为趋势类）", query);
            return QuestionType.TREND;
        }
        
        log.debug("问题类型分析: {} -> 未知类型", query);
        return QuestionType.UNKNOWN;
    }
    
    /**
     * 判断是否需要生成图表
     */
    public boolean shouldGenerateChart(String query) {
        QuestionType questionType = analyzeQuestionType(query);
        boolean needsChart = questionType.needsChart();
        
        log.info("问题: {}, 类型: {}, 是否需要图表: {}", query, questionType.getDisplayName(), needsChart);
        return needsChart;
    }
    
    /**
     * 获取推荐的图表类型
     */
    public String getRecommendedChartType(String query) {
        QuestionType questionType = analyzeQuestionType(query);
        String recommendedType = questionType.getRecommendedChartType();
        
        log.debug("问题: {}, 推荐图表类型: {}", query, recommendedType);
        return recommendedType;
    }
    
    /**
     * 检查字符串是否包含任意一个关键词
     */
    private boolean containsAny(String text, String... keywords) {
        for (String keyword : keywords) {
            if (text.contains(keyword)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取问题分析结果详情
     */
    public QuestionAnalysisResult analyzeQuestion(String query) {
        QuestionType questionType = analyzeQuestionType(query);
        boolean needsChart = questionType.needsChart();
        String recommendedChartType = questionType.getRecommendedChartType();
        
        return QuestionAnalysisResult.builder()
            .question(query)
            .questionType(questionType)
            .needsChart(needsChart)
            .recommendedChartType(recommendedChartType)
            .analysis(questionType.getDescription())
            .build();
    }
    
    /**
     * 问题分析结果
     */
    public static class QuestionAnalysisResult {
        private String question;
        private QuestionType questionType;
        private boolean needsChart;
        private String recommendedChartType;
        private String analysis;
        
        // 使用Builder模式
        public static Builder builder() {
            return new Builder();
        }
        
        public static class Builder {
            private QuestionAnalysisResult result = new QuestionAnalysisResult();
            
            public Builder question(String question) {
                result.question = question;
                return this;
            }
            
            public Builder questionType(QuestionType questionType) {
                result.questionType = questionType;
                return this;
            }
            
            public Builder needsChart(boolean needsChart) {
                result.needsChart = needsChart;
                return this;
            }
            
            public Builder recommendedChartType(String recommendedChartType) {
                result.recommendedChartType = recommendedChartType;
                return this;
            }
            
            public Builder analysis(String analysis) {
                result.analysis = analysis;
                return this;
            }
            
            public QuestionAnalysisResult build() {
                return result;
            }
        }
        
        // Getters
        public String getQuestion() { return question; }
        public QuestionType getQuestionType() { return questionType; }
        public boolean isNeedsChart() { return needsChart; }
        public String getRecommendedChartType() { return recommendedChartType; }
        public String getAnalysis() { return analysis; }
    }
}
